{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import configparser"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "### 写入配置文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 写入配置文件\n",
    "config = configparser.ConfigParser()\n",
    "\n",
    "# 数据必须 为string\n",
    "config[\"DEFAULT\"] = {'Default01': '45',\n",
    "                     'Default02': 'yes',\n",
    "                     'Default03': '测试'}\n",
    "config[\"Test\"] = {'A': '1',\n",
    "                  'B': '2.1',\n",
    "                  'C': '9'}\n",
    "\n",
    "config['Test']['B'] = '99.99'\n",
    "\n",
    "config[\"Test@\"] = {'A': '1',\n",
    "                  'B': '2.1',\n",
    "                  'C': '9'}\n",
    "#  写入后缀为.ini的文件\n",
    "with open('config.ini', 'w', encoding='utf-8') as configfile:\n",
    "    config.write(configfile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[DEFAULT]\n",
      "default01 = 45\n",
      "default02 = yes\n",
      "default03 = 娴嬭瘯\n",
      "\n",
      "[Test]\n",
      "a = 1\n",
      "b = 99.99\n",
      "c = 9\n",
      "\n",
      "[Test@]\n",
      "a = 1\n",
      "b = 2.1\n",
      "c = 9\n",
      "\n"
     ]
    }
   ],
   "source": [
    "! cat config.ini"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "### 读取配置文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[DEFAULT]\n",
      "default01 = 45\n",
      "default02 = yes\n",
      "default03 = 娴嬭瘯\n",
      "\n",
      "[Test]\n",
      "a = 1\n",
      "b = 99.99\n",
      "c = 9\n",
      "\n",
      "# 娴嬭瘯鑴氭湰 \n",
      "[Test@]\n",
      "a = 1\n",
      "b = 2.1  # 娴嬭瘯2 涓嶈兘鍐欏湪杩欓噷\n",
      "c = 9\n"
     ]
    }
   ],
   "source": [
    "! cat config_2.ini"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "默认节点==> OrderedDict([('default01', '45'), ('default02', 'yes'), ('default03', '测试')])\n",
      "所有节点==> ['Test', 'Test@']\n",
      "\n",
      "Test:\n",
      "      a         :1\n",
      "      b         :99.99\n",
      "      c         :9\n",
      "      default01 :45\n",
      "      default02 :yes\n",
      "      default03 :测试\n",
      "\n",
      "Test@:\n",
      "      a         :1\n",
      "      b         :2.1  # 测试2 不能写在这里\n",
      "      c         :9\n",
      "      default01 :45\n",
      "      default02 :yes\n",
      "      default03 :测试\n"
     ]
    }
   ],
   "source": [
    "# 读取配置文件\n",
    "config = configparser.ConfigParser()\n",
    "config.read(\"config_2.ini\", encoding='utf-8')\n",
    "\n",
    "# 默认配置  ’DEFAULT‘\n",
    "print(\"默认节点==>\",config.defaults())\n",
    "\n",
    "# 其它节点\n",
    "print(\"所有节点==>\",config.sections())\n",
    "\n",
    "# 打印所有配置参数 \n",
    "for item in config.sections():\n",
    "    print('\\n{}:'.format(item))\n",
    "    for key_i in config[item]:\n",
    "        print('      {:10s}:{}'.format(key_i, config[item][key_i]))\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "### 修改配置文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "config.add_section(\"new_section\")\n",
    "config.set(\"new_section\",\"Test3\",\"110\")\n",
    "config.write(open('config_2.ini', \"w\", encoding='utf-8'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[DEFAULT]\n",
      "default01 = 45\n",
      "default02 = yes\n",
      "default03 = 娴嬭瘯\n",
      "\n",
      "[Test]\n",
      "a = 1\n",
      "b = 99.99\n",
      "c = 9\n",
      "\n",
      "[Test@]\n",
      "a = 1\n",
      "b = 2.1  # 娴嬭瘯2 涓嶈兘鍐欏湪杩欓噷\n",
      "c = 9\n",
      "\n",
      "[new_section]\n",
      "test3 = 110\n",
      "\n"
     ]
    }
   ],
   "source": [
    "! cat config_2.ini"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
